package com.settlement.system.common.util.common;

import com.alibaba.excel.util.DateUtils;
import com.settlement.system.model.entity.common.FileByte;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;

/**
 * zip 工具
 * @author author
 */
@Slf4j
public class FileByteUtils {

    /**
     * 获取file的文件 FileByte 如果是压缩文件，解压后再获取
     * @param file
     * @return
     */
    public static List<FileByte> getFileByte(MultipartFile file) throws IOException {
        final String zipExt = ".zip";
        if(file.getOriginalFilename().endsWith(zipExt)){
            return getZipFileByte(file.getInputStream());

        }
        FileByte fileByte = new FileByte();
        fileByte.setFileName(file.getOriginalFilename());
        fileByte.setFilePath(getFilePathPrefix() + file.getOriginalFilename());
        fileByte.setContent(IOUtils.toByteArray(file.getInputStream()));
        return Arrays.asList(fileByte);
    }

    /**
     * 得到一个 fileByte
     * @param file
     * @return
     * @throws IOException
     */
    public static FileByte getSingleFileByte(MultipartFile file) throws IOException{
        FileByte fileByte = new FileByte();
        fileByte.setFileName(file.getOriginalFilename());
        fileByte.setFilePath(getFilePathPrefix() + file.getOriginalFilename());
        fileByte.setContent(IOUtils.toByteArray(file.getInputStream()));
        return fileByte;
    }

    /**
     * 获取Zip 文件的文件目录
     * @param inputStream
     * @return
     */
    private static List<FileByte> getZipFileByte(InputStream inputStream){
        List<FileByte> fileBytes = new ArrayList<>();
        String filePathPrefix = getFilePathPrefix();
        try (InputStream bis = new BufferedInputStream(inputStream);
             ArchiveInputStream ais = new ZipArchiveInputStream(bis,"GBK")) {
            ArchiveEntry entry;
            while (Objects.nonNull(entry = ais.getNextEntry())) {
                if (!ais.canReadEntryData(entry)) {
                    continue;
                }
                if(!entry.isDirectory()){
                    FileByte fileByte = new FileByte();
                    fileByte.setFilePath(filePathPrefix + entry.getName());
                    fileByte.setFileName(entry.getName().substring(entry.getName().lastIndexOf('/') + 1));
                    byte[] bytes = IOUtils.toByteArray(ais);
                    fileByte.setContent(bytes);
                    fileBytes.add(fileByte);
                }
            }
        } catch (IOException e) {
            log.error("获取数据异常",e);
        }
        return fileBytes;
    }

    private static String getFilePathPrefix(){
        String date = DateUtils.format(new Date(),"yyyy-MM-dd");
        String key = String.format("acs-file-%s",date);
        long value = RedisUtil.incr(key,1);
        return String.format("%s%s\\%s\\",AcsFileUtils.getStaticReportPath(),date,value);
    }

}
